package com.shawn.mobliesafe;

import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shawn.db.dao.AddressDao;

public class AddressActivity extends AppCompatActivity {

    private EditText et_address_queryphone;
    private TextView tv_address_queryaddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        et_address_queryphone = (EditText)findViewById(R.id.et_address_queryphone);
        tv_address_queryaddress = (TextView)findViewById(R.id.tv_address_queryaddress);
        et_address_queryphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String phone = s.toString();
                String location = AddressDao.queryAddress(phone,getApplicationContext());
                if(!TextUtils.isEmpty(location)) {
                    tv_address_queryaddress.setText(location);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void query(View view){
        String num = et_address_queryphone.getText().toString().trim();
        if (TextUtils.isEmpty(num)) {
            Toast.makeText(getApplicationContext(), "请输入要查询号码", Toast.LENGTH_SHORT).show();
            //实现抖动的效果
            Animation shake = AnimationUtils.loadAnimation(this,R.anim.shake);
            et_address_queryphone.startAnimation(shake);//开启动画
            //振动
            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(100);
            vibrator.vibrate(new long[]{20l,10l,20l,10l}, -1);
        }else {
            String location = AddressDao.queryAddress(num,this);
            if (!TextUtils.isEmpty(location)) {
                tv_address_queryaddress.setText(location);
            }else {
                tv_address_queryaddress.setText("亲，未查询到归属地");
            }
        }
    }
}
